package com.jd.repairdata;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.jd.repairdata.common.ExcelUtils;
import org.apache.commons.lang3.time.DateFormatUtils;


/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
//	    String path = "C:/Users/songjunou/Desktop/副本案件系统补数据-0320-1.xlsx";
//    	hbasedata(path);
//    	System.out.println(encryptSHA256("6222081610001279118"));
    	
    	System.out.println(UUID.nameUUIDFromBytes(("import data by Excel "+"2017031211001100656472942").getBytes()));
    	
    }




	public static String hbasecarddata(String path) {
		CodeUtils.init();
    	List<Trade> trades = new ArrayList<Trade>();
	    try {
	    	 String[][] datas = ExcelUtils.readExcel(path);
	    	for(int i = 1 ; i <datas.length ; i++)
	    	{
				Trade trade = new Trade();
				trade.setTrade_no(datas[i][1].trim());
				trade.setMerchant_trade_no(null!=datas[i][2]? datas[i][2].trim():"");
				if(null!=datas[i][3] && !datas[i][3].contains("//"))
				{
					trade.setTrade_pay_request_no(datas[i][3].trim());
				}
				trade.setBank_no(null!=datas[i][4]? datas[i][4].trim():"");
				trade.setSeller_merchant_no(null!=datas[i][5] ?datas[i][5].trim():"");
				trade.setNew_member_id(null!=datas[i][6]? datas[i][6].trim():"");
				trade.setTrade_type_code("03");
				trade.setSub_trade_type_code("01");
				if(null!= datas[i][10] && !datas[i][10].contains("//"))
				{
					trade.setAdd_goods_order_no(datas[i][10].trim());
				}
				trade.setTrade_amount(StringUtils.isNoneBlank(datas[i][11])? (long) (Float.valueOf(datas[i][11].trim())*100)+"":"");
				trade.setCurrency_code(null!=datas[i][12] ? datas[i][12].trim():"");
				trade.setTran_start_time(null!=datas[i][13]? datas[i][13].trim():"");
				trade.setCreated_date(DateFormatUtils.format(new Date() , "yyyy-MM-dd HH:mm:ss"));
				trade.setCard_pan(StringUtils.isNoneBlank(datas[i][14]) ?encryptSHA256(datas[i][14].trim()) :"");
				if(null!=datas[i][15] && datas[i][15].equals("借记卡"))
				{
					trade.setCard_type_code("D");
				}else{
					trade.setCard_type_code("C");
				}
				trade.setHolder_id_number(null!=datas[i][16]? datas[i][16].trim() :"");
				trade.setHolder_name(null!=datas[i][17] ? datas[i][17].trim():"");
				trade.setMobile1(null!=datas[i][18] ? datas[i][18].trim():"");
				trade.setCard_bank(StringUtils.isNoneBlank(datas[i][19]) ?CodeUtils.getBankCode(datas[i][19].trim()):"");
				String key = UUID.nameUUIDFromBytes(("jdjr_song_casesystem_20161131"+trade.getTrade_no()).getBytes()).toString();
				trade.setPrimary_key(key);
				trade.setPayment_code("02");
				trades.add(trade);
	    	}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    System.out.println(JSON.toJSON(trades));
	    return JSON.toJSON(trades).toString();
	}
    
    
    
    
    /**
	 * 
	 * <p>
	 * Description: SHA-256加密
	 * </p>
	 * 
	 * @param orginanlValue
	 * @return
	 * @author SSH
	 * @date 2013年9月4日
	 */
	public static String encryptSHA256(String orginanlValue)  {
		
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("SHA-256");
			messageDigest.reset();
			messageDigest.update(orginanlValue.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		byte[] byteArray = messageDigest.digest(); // 进行加密运算并返回字符数组

		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) { // 字节数组转换成十六进制字符串，形成最终的密文
			int v = byteArray[i] & 0xff;
			if (v < 16) {
				buffer.append(0);
			}
			buffer.append(Integer.toHexString(v));
		}
		return buffer.toString();

	}




	public static String hbaseaccountdata(String path) {
		CodeUtils.init();
    	List<Trade> trades = new ArrayList<Trade>();
	    try {
	    	 String[][] datas = ExcelUtils.readExcel(path);
	    	for(int i = 1 ; i <datas.length ; i++)
	    	{
				Trade trade = new Trade();
				trade.setTrade_no(datas[i][1].trim());
				trade.setMerchant_trade_no(null!=datas[i][2]? datas[i][2].trim():"");
				trade.setBank_no(null!=datas[i][3]? datas[i][3].trim():"");
				if(null!=datas[i][4] && !datas[i][4].contains("//"))
				{
					trade.setTrade_pay_request_no(datas[i][4].trim());
				}
				trade.setCard_pan(StringUtils.isNoneBlank(datas[i][6]) ?encryptSHA256(datas[i][6].trim()) :"");
				trade.setCard_bank(StringUtils.isNoneBlank(datas[i][7]) ?CodeUtils.getBankCode(datas[i][7].trim()):"");
				if(null!=datas[i][8] && datas[i][8].equals("借记卡"))
				{
					trade.setCard_type_code("D");
				}else{
					trade.setCard_type_code("C");
				}
				trade.setNew_member_id(null!=datas[i][9]? datas[i][9].trim():"");
				trade.setSeller_merchant_no(null!=datas[i][11] ?datas[i][11].trim():"");
				trade.setTrade_type_code("19");
				trade.setSub_trade_type_code("08");
				trade.setTrade_amount(StringUtils.isNoneBlank(datas[i][13])? (long) (Float.valueOf(datas[i][13].trim())*100)+"":"");
				trade.setCurrency_code("CNY");
				trade.setTran_start_time(null!=datas[i][15]? datas[i][15].trim():"");
				trade.setCreated_date(DateFormatUtils.format(new Date() , "yyyy-MM-dd HH:mm:ss"));
				trade.setJd_pin(null!=datas[i][16] ? datas[i][16].trim():"");
				String key = UUID.nameUUIDFromBytes(("jdjr_song_casesystem_20161131"+trade.getTrade_no()).getBytes()).toString();
				trade.setPrimary_key(key);
				trade.setPayment_code("02");
				trades.add(trade);
	    	}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    System.out.println(JSON.toJSON(trades));
	    return JSON.toJSON(trades).toString();
	}
}
